package com.longshine.platform.equip.tcp;

import com.longshine.charge.auto.downcomp.Position;
import com.longshine.tools.util.NumberUtil;
import com.longshine.tools.util.StrTmpUtil;


public class ProtocolUtil {
	
	
	public final static String PROTOCOL_HEAD = "68";
	public final static String PROTOCOL_VERSION = "2134";
	public final static String PROTOCOL_TERMINAL = "$Ter";
	public final static String PROTOCOL_LEN = "$LEN";
	public final static String PROTOCOL_FLAG = "C0";
	public final static String PROTOCOL_FUNCTION = "$F";
	public final static String PROTOCOL_DATAAREA = "$D";
	public final static String PROTOCOL_TAIL = "16";
	
	public final static String PROTOCOL_MODE = PROTOCOL_HEAD + PROTOCOL_VERSION
			+ PROTOCOL_LEN + PROTOCOL_TERMINAL + PROTOCOL_FLAG + PROTOCOL_FUNCTION + PROTOCOL_DATAAREA + PROTOCOL_TAIL;
	
	
	public static String createProtocol(int terminalAddr, int taskId,
			int systemType, int funcCode, String dataArea) {
		String ret = PROTOCOL_MODE;
		String sTer = StrTmpUtil.tcpStrExchangeSeqence(NumberUtil.toHexStr(terminalAddr, 2));
		ret = ret.replace(PROTOCOL_TERMINAL, sTer);
		ret = ret.replace(PROTOCOL_FUNCTION, NumberUtil.toHexStr(funcCode, 1));
		ret = addTaskId(ret,taskId);
		ret = replaceSystem(ret,systemType,terminalAddr);
		ret = ret.replace(PROTOCOL_DATAAREA, dataArea == null ? "":dataArea);
		int len = PROTOCOL_TERMINAL.length()+PROTOCOL_FLAG.length()+PROTOCOL_FUNCTION.length()+(dataArea==null?0:dataArea.length()) + PROTOCOL_TAIL.length();
		String sLen = StrTmpUtil.tcpStrExchangeSeqence(NumberUtil.toHexStr(len/2, 2));
		ret = ret.replace(PROTOCOL_LEN, sLen);
		return ret;
	}
	
	public static int readLength(String recv){
		int start = (PROTOCOL_HEAD+PROTOCOL_VERSION+PROTOCOL_TERMINAL).length();
		return NumberUtil.hex2Decimal(StrTmpUtil.tcpStrExchangeSeqence(recv.substring(start,start + 4)));
	}
	
	public static Position getPositions(String recv){
		String dataArea = ProtocolUtil.getDataAreaString(recv);
		String[] pos = StrTmpUtil.splitLength(dataArea, 8);
		Position ret = new Position();
		ret.xPos = NumberUtil.hex2Float(StrTmpUtil.tcpStrExchangeSeqence(pos[0]));
		ret.yPos = NumberUtil.hex2Float(StrTmpUtil.tcpStrExchangeSeqence(pos[1]));
		ret.zPos = NumberUtil.hex2Float(StrTmpUtil.tcpStrExchangeSeqence(pos[2]));
		ret.aRot = NumberUtil.hex2Float(StrTmpUtil.tcpStrExchangeSeqence(pos[3]));
		ret.bRot = NumberUtil.hex2Float(StrTmpUtil.tcpStrExchangeSeqence(pos[4]));
		ret.cRot = NumberUtil.hex2Float(StrTmpUtil.tcpStrExchangeSeqence(pos[5]));
		return ret;
	}
	
	public static void main(String args[]){
		System.out.println(getFunctionData("68214305000100C40416"));
	}
	
	public static int getTaskId(String code){
		String t = code.substring(14,16);
		String bStr = NumberUtil.decimal2Binary(NumberUtil.hex2Decimal(t));
		String st = bStr.substring(bStr.length()-2);
		int type = (int)NumberUtil.binary2Decimal(st);
		return type;
	}
	public static String replaceSystem(String code,int systemType,int terminalId){
		String head = code.substring(0,5*2);
		
		String tail = code.substring(5*2+4);
		String sTerm = StrTmpUtil.tcpStrExchangeSeqence(NumberUtil.toHexStr(terminalId,2));
		code = head + sTerm + tail;
		
		head = code.substring(0,14);
		String t = code.substring(14,16);
		t = NumberUtil.formatBinaryStr(NumberUtil.hex2Binary(t),8);
		String tHead = t.substring(0,2);
		String sSysType = NumberUtil.formatBinaryStr(NumberUtil.decimal2Binary(systemType),4);
		String tTaskId = t.substring(6);
		t = NumberUtil.binary2Hex(tHead + sSysType +tTaskId,1);
		tail = code.substring(16);
		code = head + t +tail;
		return code;
	}
	
	public static int getSystemType(String code){
		String head = code.substring(14,16);
		head = NumberUtil.formatBinaryStr(NumberUtil.hex2Binary(head),8);
		head = head.substring(2,6);
		return (int)NumberUtil.binary2Decimal(head);
	}
	
	public static int getTerminalCode(String code){
		String head = code.substring(5*2,5*2+4);
		head = StrTmpUtil.tcpStrExchangeSeqence(head);
		return Integer.parseInt(head);
	}
	
	public static int getFunctionData(String code){
		return NumberUtil.hex2Decimal(code.substring((1+2+2+2+1)*2,16+2));
	}
	
	public static String addTaskId(String code,int taskId){
		String head = code.substring(0,14);
		String t = code.substring(14,16);
		String tail = code.substring(16);
		String bStr = NumberUtil.decimal2Binary(NumberUtil.hex2Decimal(t));
		String sh = bStr.substring(0,2);
		String st = bStr.substring(2);
		int type = (int)NumberUtil.binary2Decimal(st);
		st = NumberUtil.formatBinaryStr(NumberUtil.decimal2Binary(type), 4);
		String sTask = NumberUtil.formatBinaryStr(NumberUtil.decimal2Binary(taskId), 2);
		String newStr = NumberUtil.binary2Hex(sh + st + sTask, 1);
		return head +newStr + tail;
	}
	
	public static String getDataAreaString(String code){
		String len = code.substring(6,6+4);
		len = StrTmpUtil.tcpStrExchangeSeqence(len);
		int l = NumberUtil.hex2Decimal(len);
		l = l - 5;
		if(l  <= 0) return null;
		return code.substring(18,18+l*2);
	}
}
